package com.bjbn.tasks;

import com.bjbn.service.HistoryDataCleanupService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

/**
 * 历史数据清理定时任务
 */
@Slf4j
@Component
public class HistoryDataCleanupTask {

    @Autowired
    private HistoryDataCleanupService historyDataCleanupService;

    /**
     * 每天凌晨2点执行历史数据清理
     * 清理一个月之前的温湿度和照度传感器历史数据
     */
    @Scheduled(cron = "0 0 2 * * ?")
    public void cleanupHistoryData() {
        log.info("开始执行历史数据清理定时任务");
        
        try {
            String result = historyDataCleanupService.cleanupAllHistoryData();
            log.info("历史数据清理定时任务执行完成: {}", result);
        } catch (Exception e) {
            log.error("历史数据清理定时任务执行异常", e);
        }
    }


    /**
     * 每日凌晨3点执行温湿度传感器历史数据清理
     */
    @Scheduled(cron = "0 0 3 * * ?")
    public void cleanupThsHistoryData() {
        log.info("开始执行温湿度传感器历史数据清理定时任务");
        
        try {
            int deletedCount = historyDataCleanupService.cleanupThsHistoryData();
            log.info("温湿度传感器历史数据清理定时任务执行完成，删除{}条记录", deletedCount);
        } catch (Exception e) {
            log.error("温湿度传感器历史数据清理定时任务执行异常", e);
        }
    }

    /**
     * 每日凌晨4点执行照度传感器历史数据清理
     */
    @Scheduled(cron = "0 0 4 * * ?")
    public void cleanupLxHistoryData() {
        log.info("开始执行照度传感器历史数据清理定时任务");
        
        try {
            int deletedCount = historyDataCleanupService.cleanupLxHistoryData();
            log.info("照度传感器历史数据清理定时任务执行完成，删除{}条记录", deletedCount);
        } catch (Exception e) {
            log.error("照度传感器历史数据清理定时任务执行异常", e);
        }
    }



} 